//
//  AppDelegate.swift
//  weibo
//
//  Created by jason on 17/2/21.
//  Copyright © 2017年 jason. All rights reserved.
//

import UIKit
import UserNotifications
@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate {

    var window: UIWindow?


    func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
        
        //取得用户授权的显示通知,提示音/声音/bagenumber
        if #available(iOS 10.0, *) {
            UNUserNotificationCenter.current().requestAuthorization(options: [.sound,.carPlay,.badge,.alert]) { (success, error) in
                print("授权"+(success ? "成功":"失败"))
            }
            } else {
            // Fallback on earlier versions
            let notifysettings = UIUserNotificationSettings(types: [.alert,.badge,.sound], categories: nil)
            application.registerUserNotificationSettings(notifysettings)
            
        }
       
        //睡2秒(启动画面到进入主界面等待2s)
        sleep(2)
        //实例化window
        window = UIWindow()
        //设置window的背景颜色
        window?.backgroundColor = UIColor.white
        //window的根视图控制器
        window?.rootViewController = WBMainViewController()
        //使被使用对象的主窗口显示到屏幕的最前端
         window?.makeKeyAndVisible()
        loadAppInfo()
        // Override point for customization after application launch.
        return true
    }

    }
//从服务器加载程序信息
extension AppDelegate{
    //
    fileprivate func loadAppInfo() {
    //异步
        DispatchQueue.global().async {
            //1url
            let url = Bundle.main.url(forResource: "url.json", withExtension: nil)
            //2data
            let data = NSData.init(contentsOf: url!)
            //3写入磁盘
            let docdir = NSSearchPathForDirectoriesInDomains(.documentDirectory, .userDomainMask, true)[0]
            let jsonpath = (docdir as NSString).appendingPathComponent("url.json")
            //直接保存在沙盒,等待下次使用
            data?.write(toFile: jsonpath, atomically: true)
            print("网络数据加载成功")
        }
    }
}

